/**
 * 
 */
package com.kgcx.schedule.rabbitmq.consumer;

import com.kgcx.common.client.order.OrderClient;
import com.kgcx.common.client.order.pojo.Order;
import com.kgcx.common.constant.Cons;
import com.kgcx.common.eum.OrderEnum;
import com.kgcx.common.rabbitmq.consts.QueueConst;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.schedule.domain.ScheduleRecordDomain;
import com.kgcx.schedule.service.ScheduleOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 袁进勇
 *
 */
@Component

public class OrderCreatedConsumer extends AbstractScheduleConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(OrderCreatedConsumer.class);

    @Autowired
    private OrderClient orderClient;

    @RabbitListener(queues = QueueConst.SCHEDULE_ORDER_CREATE, errorHandler = "custRabbitListenerErrorHandler")
    public void orderCreated(String orderJson) {
        LOG.info("收到订单创建的MQ消息 。\n" + orderJson);
        Order order = JsonUtil.toObject(orderJson, Order.class);

        // 如果订单已经取消了，则忽略掉。
        Order o = orderClient.getByOrderNo(order.getOrderNo());
        if (OrderEnum.CANCEL_ORDER == o.getOrderState()) {
            LOG.info(String.format("订单[%s]，忽略，订单状态为已取消。", order.getOrderNo()));
            return;
        }

        ScheduleRecordDomain record = buildScheduleRecordDomain(order);

        ScheduleOrderService scheduleOrderService = getScheduleOrderService(record.getOrder_type());
        if (scheduleOrderService == null) {
            LOG.error(String.format("订单[%s]，处理失败，未知的订单类型[%d]！", record.getOrder_no(), record.getOrder_type()));
            return;
        }

        scheduleOrderService.addRecord(record);
    }

    private ScheduleRecordDomain buildScheduleRecordDomain(Order order) {
        ScheduleRecordDomain record = new ScheduleRecordDomain();
        record.setCity_id(Long.valueOf(order.getPlanOrigCityId()));
        record.setAirport_id(order.getAirportId());
        record.setStation_id(order.getStationId());
        record.setOrder_no(order.getOrderNo());
        record.setOrder_type(order.getOrderType());
        record.setAuto_type(order.getBookModel());
        record.setOrder_channel(order.getOrderChannel());
        record.setPlacing_user_id(order.getPlacingUserId());
        record.setPlacing_user_phone(order.getPlacingUserPhone());
        // record.setPassenger_id(null);
        record.setPassenger_phone(order.getPassengerPhone());
        record.setIs_msg_passenger(Cons.$true(order.getIsNotify()) ? Cons.BOOLEAN_TRUE : Cons.BOOLEAN_FALSE);
        record.setPlan_depart_time(
                order.getPlanDepartTime() == null ? HelpUtil.getNowTime() : order.getPlanDepartTime());
        record.setPlan_orig_city_id(Long.valueOf(order.getPlanOrigCityId()));
        record.setPlan_orig(order.getPlanOrig());
        record.setPlan_orig_lng(order.getPlanOrigLng());
        record.setPlan_orig_lat(order.getPlanOrigLat());
        record.setPlan_dest_city_id(Long.valueOf(order.getPlanDestCityId()));
        record.setPlan_dest(order.getPlanDest());
        record.setPlan_dest_lng(order.getPlanDestLng());
        record.setPlan_dest_lat(order.getPlanDestLat());
        record.setPlan_mileage(order.getPlanMileage());
        record.setPlan_duration(Long.valueOf(order.getPlanDuration()));
        // record.setActual_finish_time(null); // 实际结束时间，用于延时解锁司机
        // record.setSchedule_count(0);
        // record.setFisrt_schedule_time(null);
        // record.setLatest_schedule_time(null);
        // record.setAuto_id(null);
        // record.setAuto_no(null);
        // record.setDriver_id(null);
        // record.setDriver_name(null);
        // record.setDriver_phone(null);
        // record.setSchedule_status(ScheduleRecordDomain.STATUS_PENDING);
        // record.setCreate_time(HelpUtil.getNowTime());

        return record;
    }
}
